<?php

namespace app\models;

use think\Model;

/**
 * Class CustomerModel.
 */
class CustomerModel extends Model
{
    protected $table = 'edu_customer';

    /**
     * 客户列表.
     *
     * @param $param
     *
     * @throws \think\exception\DbException
     *
     * @return array
     */
    public function getCustomerLists($param)
    {
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = [];
        if (isset($param['campus_name']) && '' !== $param['campus_name']) {
            $where['campus_name'] = ['like', $param['campus_name'].'%'];
        }
        if (isset($param['status']) && '' !== $param['status']) {
            $where['status'] = $param['status'];
        }
        if (isset($param['type']) && '' !== $param['type']) {
            $type = $param['type'];
        } else {
            $type = 'all';
        }
        $time = '';
        if (1 !== get_current_group_id() && 2 !== get_current_group_id()) {
            $where['campus_id'] = get_current_campus_id();
        }
        switch ($type) {
            case 'today':
                //今天待跟进
                $date = date('Y-m-d');
                $start = $date.' 00:00:00';
                $end = $date.' 23:59:59';
                $where['follow_up_date'] = [['egt', $start], ['lt', $end]];
                $where['status'] = 1;

                break;
            case 'tomorrow':
                // 明天待跟进
                $date = date('Y-m-d', strtotime('+1 day'));
                $start = $date.' 00:00:00';
                $end = $date.' 23:59:59';
                $where['follow_up_date'] = [['egt', $start], ['lt', $end]];
                $where['status'] = 1;

                break;
            case 'week':
                //本周待跟进
                $time = 'week';
                $where['status'] = 1;

                break;
            case 'my':
                //我的客户
                $where['user_id'] = get_current_id();
                $where['status'] = 1;

                break;
            case 'public':
                //公共客户
                $where['user_id'] = 0;
                $where['status'] = 1;

                break;
            case 'invalid':
                //无效客户
                $where['status'] = -1;

                break;
            case 'assigned':
                //已分配客户
                $where['user_id'] = ['neq', get_current_id()];
                $where['status'] = 1;

                break;
            case 'all':
                break;
        }
        if ($time) {
            $listData = self::where($where)->whereTime('follow_up_date', $time)
                ->paginate($listRows, false, ['page' => $page])
                ->toArray();
        } else {
            $listData = self::where($where)
                ->paginate($listRows, false, ['page' => $page])
                ->toArray();
        }
        if (!empty($listData)) {
            foreach ($listData['data'] as &$item) {
                $channel = DictionaryModel::get($item['channel'])['name'];
                $item['channel'] = $channel ? $channel : '-';
                $item['follow_up_text'] = self::getFollowUpText($item['follow_up']);
                if (1 === $item['intentional_grade']) {
                    $item['intentional_grade_text'] = '<i class="layui-icon" style="color: yellow">&#xe67a;</i>';
                } elseif (2 === $item['intentional_grade']) {
                    $item['intentional_grade_text'] = '<i class="layui-icon" style="color: yellow">&#xe67a;</i>
                                                        <i class="layui-icon" style="color: yellow">&#xe67a;</i>';
                } elseif (3 === $item['intentional_grade']) {
                    $item['intentional_grade_text'] = '<i class="layui-icon" style="color: yellow">&#xe67a;</i>
                                                        <i class="layui-icon" style="color: yellow">&#xe67a;</i>
                                                        <i class="layui-icon" style="color: yellow">&#xe67a;</i>';
                } elseif (4 === $item['intentional_grade']) {
                    $item['intentional_grade_text'] = '<i class="layui-icon" style="color: yellow">&#xe67a;</i>
                                                        <i class="layui-icon" style="color: yellow">&#xe67a;</i>
                                                        <i class="layui-icon" style="color: yellow">&#xe67a;</i>
                                                        <i class="layui-icon" style="color: yellow">&#xe67a;</i>';
                } elseif (5 === $item['intentional_grade']) {
                    $item['intentional_grade_text'] = '<i class="layui-icon" style="color: yellow">&#xe67a;</i>
                                                        <i class="layui-icon" style="color: yellow">&#xe67a;</i>
                                                        <i class="layui-icon" style="color: yellow">&#xe67a;</i>
                                                        <i class="layui-icon" style="color: yellow">&#xe67a;</i>
                                                        <i class="layui-icon" style="color: yellow">&#xe67a;</i>';
                }
            }
        }

        return $listData;
    }

    /**
     * 跟进状态
     * @param $follow_up
     * @return mixed
     */
    public function getFollowUpText($follow_up)
    {
        $value = '';
        switch ($follow_up){
            case -1:
                $value = '未处理';
                break;
            case -2:
                $value = '已丢单';
                break;
            case 1:
                $value = '已联系';
                break;
            case 2:
                $value = '预约试听';
                break;
            case 3:
                $value = '邀约上门';
                break;
            case 4:
                $value = '已交订金';
                break;
        }
        return $value;
    }

    /**
     * 有效、无效
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getStatusTextAttr($value, $data)
    {
        $arr = config('customer.status');
        return $arr[$data['status']];
    }

    /**
     * 性别.
     *
     * @param $value
     * @param $data
     *
     * @return mixed
     */
    public function getSexAttr($value, $data)
    {
        $sex = ['1' => '男', 2 => '女'];

        return $sex[$data['sex']];
    }
}
